package com.edgar.download;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by dell on 2015/9/28.
 * //线程池服务
 */
public class DownloadService extends Service{

    public static final int MAX_QUEUE_SIZE = 2;   //Max download queue is 2.
    private volatile boolean mActivate = false;  //队列线程激活状态
    private final LinkedList<DownloadTask> mDownloadTaskList = new LinkedList<DownloadTask>();
    private DownloadManager mDownloadManager;
    private QueueThread mQueueThread;

    private NotificationManager mNotificationManager;
    private ThreadPoolExecutor mDownloadExecutor = new ThreadPoolExecutor(MAX_QUEUE_SIZE,MAX_QUEUE_SIZE,
            10, TimeUnit.MILLISECONDS,new LinkedBlockingQueue()){
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            mDownloadTaskList.remove(r);
        }
    };

    @Override
    public void onCreate(){
        Log.e(Constant.TAG,"onCreate.");
        mDownloadManager = DownloadManager.getInstance();
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startQueueThread();
        Log.e(Constant.TAG, "onStartCommand.");
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy(){
        Log.e("","destroy service");
    }

    private void updateNotifyStateBar(){
//        Notification notification = new Notification();
//        notification.contentView = null;
    }

    private void startQueueThread(){
        if(!mActivate){
            if(mQueueThread != null){
                //释放上次的对象引用
                mQueueThread = null;
            }
            //new queue thread object
            mQueueThread = new QueueThread();
            mQueueThread.start();
        }
    }

    private class QueueThread extends Thread{
        @Override
        public void run() {
            mActivate = true;
            Download download = null;
            while ((download = mDownloadManager.pollNextTask()) != null){
                Log.e(Constant.TAG, "Run work queue thread.");
                synchronized (mDownloadTaskList){
                    DownloadTask downloadTask = new DownloadTask(getApplicationContext(),mDownloadManager,download);
                    if(!mDownloadTaskList.contains(downloadTask)){
                        mDownloadTaskList.add(downloadTask);
                        mDownloadExecutor.execute(downloadTask);
                    }
                    updateNotifyStateBar();
                }
            }
            mActivate = false;
        }
    }
}